function content0LoadFunction() {
	$('itemID').value = null;	
	x_getCurrentEvents(renderCurrentEvents);
}	

function content1LoadFunction() {
	var eID = $('itemID').value;
	if (eID == '') {
		$('registrationList').innerHTML = 'You haven\'t selected an event for which to view registrations.<br />Click the View Events tab and select an event.';
	} else {
		x_getRegistered(eID,returnGetRegistered);
	}	
}

function returnGetRegistered(string) {
	$('registrationList').innerHTML = string;
}

function showRegistered(eID) {
	$('itemID').value = eID;
	Example.toggleContent('content1',0,0);
}

function resendInfo(eID) {
	x_resendInfo(eID,returnResendInfo);
}

function returnResendInfo(string){
	//alert(string);
	$('buttonResend').className = 'successMessage';
	$('buttonResend').innerHTML = 'Event information has been resent.';
}	

function returnGetFiles(string) {
	var optionsArray = string.split('|');	
	var newOptionsArray = new Array();
	
	for(a=0;a<=((optionsArray.length)-1);a++) {
		newOptionsArray[a] = [optionsArray[a]];
	}	
	
	Ext.namespace('Ext.fileData');
	Ext.fileData.Files = newOptionsArray;
	
	// Populate files combo-box
	var fileStore = new Ext.data.SimpleStore({
        fields: ['file'],
        data: Ext.fileData.Files
    });
    var combo = new Ext.form.ComboBox({
        store:         fileStore,
        displayField:  'file',
        typeAhead:     true,
        mode:          'local',
        triggerAction: 'all',
        emptyText:     'Select existing file...',
        selectOnFocus: true,
        resizable:     true,
        shadow:        false,
        grow:          true,
        growMax:       400,
        growMin:       300,
        listWidth:     383,
        handleHeight:  12,
        name:          'attachFile'
    });
	
	// Set file combo box
	combo.applyTo('attachFile');
}

function renderCurrentEvents(string) {
	$('currentEvents').innerHTML = string;
}

function clearForm() {
	$('eCat').options[0].selected = true;
	$('eTitle').value = '';	
	tinyMCE.getInstanceById('eDesc').getBody().innerHTML = '';	
	$('eDateStart').value = '';
	$('eDateEnd').value = '';
	$('eURL').value = 'http://';
	$('eRegURL').value = 'http://';
	$('eLocation').value = '';
}

function addEvent() {
	// Turn off add button
	$('buttonNewEvent').style.display = 'none';
	
	// Set formAction to 'add'
	$('formAction').value = 'add';
	
	// Clear form
	clearForm();
        	
	// Display add form
	new Effect.SlideDown('formNewEvents', {direction: 'top', duration: .3});	
}

function cancelNewEvent() {
	// Display add form
	new Effect.SlideUp('formNewEvents', {direction: 'top', duration: .2});
	// Clear form
	clearForm();
	// Turn on add button
	$('buttonNewEvent').style.display = 'block';	
}

function saveEvent() {
	var eID = $('eID').value;
	var formAction   = $('formAction').value;
	
	var eCat         = $('eCat').options;
	var eCat         = eCat[eCat.selectedIndex].value;
	var eTitle       = $('eTitle').value;
	var eDesc        = tinyMCE.getContent(eDesc);
	var eLocation    = $('eLocation').value;
	var eURL         = $('eURL').value;
	var eDateStart   = $('eDateStart').value;
	var eDateEnd     = $('eDateEnd').value;
	var eTimes       = $('eTimes').value;
	var attachedFile = $('attachFile').value;
	var eReg         = $('eReg');
	if (eReg.checked == true) { eReg = 1; }
	else { eReg = 0; }
	var eRegURL      = $('eRegURL').value;	
	
	// Do some error checking
	if (eTitle == '') {
		var error = 1;
		var message = 'You must enter a Title.';
	}
	if (eDateStart == '') {
		var error = 1;
		var message = 'You must enter a Start Date.';
	}
	if ( (eURL.substring(0,7) != 'http://') && (eURL != '') ) {
		var error = 1;
		var message = 'All URL\'s must begin with http://.';
	}
	if ( (eRegURL.substring(0,7) != 'http://') && (eRegURL != '') ) {
		var error = 1;
		var message = 'All URL\'s must begin with http://.';
	}
	
	if (error != 1) {
		$('formStatus').className = '';
		$('formStatus').innerHTML = '';
		var phpString  = eCat+'|'+eTitle+'|'+eDesc+'|'+eLocation+'|'+eURL+'|'+eDateStart+'|'+eDateEnd+'|'+eTimes+'|'+attachedFile+'|'+eReg+'|'+eRegURL+'|'+eID;
		x_saveEvent(phpString,formAction,returnSaveEvent);	
	} else {
		$('formStatus').className = 'errorMessage';
		$('formStatus').innerHTML = message;
	}	
}

function returnSaveEvent() {
	// Do some post processing here if needed and then...
	
	// Close the add new event form
	cancelNewEvent();
	
	// Render our current events
	x_getCurrentEvents(renderCurrentEvents);
}	

function returnDeleteEvent() {
	x_getCurrentEvents(renderCurrentEvents);
}

function editEvent(eID) {
	x_getEventForEdit(eID,returnGetEventForEdit);
}

function returnGetEventForEdit(string) {
	// Parse string
	stringArray = string.split("|");
	var eID        = stringArray[0];
	var eCat       = stringArray[1];
	var eTitle     = stringArray[2];
	var eLocation  = stringArray[3];
	var eDesc      = stringArray[4];
	var eURL       = stringArray[5];
	var eRegURL    = stringArray[6];
	var eDateStart = stringArray[7];
	var eDateEnd   = stringArray[8];
	var eTimes     = stringArray[9];
	var eReg       = stringArray[10];
	var eFile      = stringArray[11];
	
	// Populate add/edit form	
	var eCatOptions = $('eCat').options;
	for(a=0;a<eCatOptions.length;a++) {
		if ($('eCat').options[a].value == eCat) {
			$('eCat').options[a].selected = true;
		}
	}
		
	$('eTitle').value = eTitle;
	$('eDesc').value = eDesc;
	tinyMCE.getInstanceById('eDesc').getBody().innerHTML = eDesc;	
	$('eLocation').value  = eLocation;	
	$('eURL').value       = eURL;	
	$('eDateStart').value = eDateStart;
	$('eDateEnd').value   = eDateEnd;	
	$('eTimes').value     = eTimes;
	$('attachFile').value = eFile;
	
	if (eReg == 1) { $('eReg').checked = true; }
	else { $('eReg').checked = false; }
	$('eRegURL').value = eRegURL;	
	
	// Set formAction to 'edit'
	$('formAction').value = 'edit';
	
	// Set eID
	$('eID').value = eID;
		
	// Turn off add button
	$('buttonNewEvent').style.display = 'none';
	
	// Display add form
	new Effect.SlideDown('formNewEvents', {direction: 'top', duration: .3});	
}

function swapOnHome(eID) {
	x_swapOnHome(eID,returnSwapOnHome);
}

function returnSwapOnHome(string) {
	stringArray = string.split("|");
	
	var eID     = stringArray[0];
	var content = stringArray[1];
	
	$('onHomeDiv'+eID).innerHTML = content;
}

function swapActive(eID) {
	x_swapActive(eID,returnSwapActive);
}

function returnSwapActive(string) {
	stringArray = string.split("|");
	
	var eID     = stringArray[0];
	var content = stringArray[1];
	
	$('onHomeDiv'+eID).innerHTML = content;
}

function saveReg(doWhat,regUserID,eID) {
	// First determine if we have a program selected yet
	var programID = $('program'+regUserID).options;
	var programID = programID[programID.selectedIndex].value;
	if ( (programID == 'Select...') || (programID == '') ) {
		$('message1').className = 'errorMessage';
		$('message1').innerHTML = 'You must select a program first.';
		doFade(7,'message1');
	} else {	
		if (doWhat == 'add') {
			var nameFirst = $('newNameFirst').value;
			var nameLast  = $('newNameLast').value;
			var phone     = $('newPhone').value;
			var email     = $('newEmail').value;
			var position  = $('newPosition').options;
			var position  = position[position.selectedIndex].value;
			var phpString = programID+'|'+nameFirst+'|'+nameLast+'|'+phone+'|'+email+'|'+position;
		} else {
			var nameFirst = $('nameFirst'+regUserID).value;
			var nameLast  = $('nameLast'+regUserID).value;
			var phone     = $('phone'+regUserID).value;
			var email     = $('email'+regUserID).value;
			var position  = $('position'+regUserID).options;
			var position  = position[position.selectedIndex].value;
			var emailHidden = $('emailHidden'+regUserID).value;
			var phpString = programID+'|'+nameFirst+'|'+nameLast+'|'+phone+'|'+email+'|'+position+'|'+emailHidden;
		}
		
		// More error checking
		if ( (nameFirst == 'First Name') || 
			 (nameLast == 'Last Name')  || 
			 (nameFirst == '') || 
			 (nameLast == '') ) {
			
			$('message1').className = 'errorMessage';
			$('message1').innerHTML = 'You must enter a First and Last Name.';
			doFade(7,'message1');
		} else {	
			clearMessage('message1');	
			x_saveReg(phpString,eID,doWhat,regUserID,returnSaveReg);
		}	
	}	
}

function returnSaveReg(string) {
	var stringArray = string.split('|');
	var content = stringArray[0];	
	var uID     = stringArray[1];	
	if (content != 'null') {		
		var newPersonDiv = document.createElement("div");
		newPersonDiv.id  = 'person'+uID;
		newPersonDiv.innerHTML = content;
		newPersonDiv.style.padding = '3px';
		var newPerson = $('newPerson');
		var registerPeopleContainer = $('registerPeopleContainer');
      	registerPeopleContainer.insertBefore(newPersonDiv,newPerson);
		
		// Clear form
		clearRegForm();
		
		// Turn on label
		if ($('existingLabel').style.display == 'none') {
			$('existingLabel').style.display = 'block';
		}
	}
	doFade(7,'row'+uID);		
}

function deleteReg(uID,eID) {
	Effect.Fade('row'+uID, {delay: 1, duration: 3});
	//$('row'+uID).style.display = 'none';
	x_deleteReg(uID,eID,returnDeleteReg);
}

function returnDeleteReg() {
	// Do nothing
}

function openAddEditDlg(eID) {
	var element = $('editButton'+eID);
    var dialog = new Ext.BasicDialog("dlgFormNewEvents", { 
            autoTabs:  true,
            width:     700,
            height:    450,
            shadow:    false,
            minWidth:  300,
            minHeight: 250,
            proxyDrag: true,
			animate:   true			
    });
    dialog.addKeyListener(27, dialog.destroy, dialog);
    //dialog.addButton('Submit', dialog.hide, dialog).disable();
    dialog.addButton('Close', dialog.destroy, dialog);
	dialog.show();	
}

function switchDisplay() {
	var staticTable = $('staticTable');
	var editTable   = $('editTable');
	if (staticTable.style.display == '') {
		editTable.style.display   = '';
		staticTable.style.display = 'none';
		$('displayLink').className = 'buttonTableView';
	} else {
		editTable.style.display   = 'none';
		staticTable.style.display = '';
		$('displayLink').className = 'buttonEditView';
	}
}

function init() {
	var dateStart = new Ext.form.DateField({
		name: 'eDateStart',
		width: 150,
		allowBlank: false,
		format: 'm/d/Y'
	});
	dateStart.applyTo('eDateStart');
	
	var dateEnd = new Ext.form.DateField({
	    name: 'eDateEnd',
	    width: 150,
	    allowBlank:false,
	    format: 'm/d/Y'
	});	
	dateEnd.applyTo('eDateEnd');
	
	// Get files for event file drop-down menu
	x_getFilesFromDir('Event Files',returnGetFiles);	
}

Ext.EventManager.onDocumentReady(init);
